package com.googlecode.dgwt.client.dijit.base.state;

import java.util.Collection;
import java.util.Map;

import com.google.gwt.user.client.ui.Widget;
import com.googlecode.dgwt.client.dojo.util.StringMap;

/**
 * @author      Wellington Tadeu dos Santos
 * @since       DGWT v0.01
 */
public abstract class WidgetState {

    private static final Map<String, WidgetState> allStates = new StringMap<WidgetState>();
    private static int generateId;

    private int id;
    
    
    public WidgetState() {
        assert (allStates.size() < 31) : "WidgetState: Generated ID overflow";
        id = 1 << generateId++;
        allStates.put(""+id, this);
    }

    
    protected abstract String getDependentStyleClass();


    public void update(Widget widget, boolean activate) {
        if (activate) {
            widget.addStyleDependentName(getDependentStyleClass());
        } else {
            widget.removeStyleDependentName(getDependentStyleClass());
        }
    }
    
    public int getId() {
        return id;
    }

    @Override
    public int hashCode() {
        return id;
    }

    
    public static WidgetState findById(int id) {
        return allStates.get(""+id);
    }
    
    public static Collection<WidgetState> getAllWidgetStates() {
        return allStates.values();
    }

}
